import Axios from 'axios'

const state = {
	loading: [true],
	tableData: [],
	currentPage: [1],		//当前页数
	pageSize: [15],			//每页显示的条目数
	total: [],				//总条目数
	multipleSelection: [],
	selectRowId: [],
	status: [],		//0 === 普通数据分页   1 === 搜索结果进行分页
	searchResult: [],		//搜索结果
}

const mutations = {
	updateStatus(state, n) {
		state.status.splice(0, 1, n);
	},
	updateCurrentPage(state, n) {			//当前页数
		state.currentPage.splice(0, 1, n);
	},
	updateTotal(state, sum) {				//更新总条目数
		state.total.splice(0, 1, sum);
	},
	updateTableData(state, arr) {			//更新表格数据
		let tableData = state.tableData;
		tableData.length = 0;
		if(arr && arr.length > 0) {
			arr.forEach(item => tableData.push(item));
		} else {
			tableData.splice(0);
		}
	},
	multipleSelection(state, arr) {
        state.multipleSelection.length = 0;
        state.selectRowId.length = 0;
        if(arr.length > 0) {
            arr.forEach(item => {
                state.multipleSelection.push(item);
                state.selectRowId.push(item['id']);
            });
        } else {
            state.multipleSelection.splice(0);
            state.selectRowId.splice(0);
        }
    },
    updateSearchResult(state, arr) {		//更新查询结果数据
    	let searchResult = state.searchResult;
    	searchResult.length = 0;
    	state.searchResult.splice(0, 1, ...arr);
    }
}

const actions = {
	getTableData({state, commit}, startPage) {			//获取表格数据
		state.loading.splice(0, 1, true);
		startPage = startPage || 1;
		Axios.get('openrationLog/selectOpenrationLogforList?page=' + startPage + '&pageSize=' + state.pageSize[0]).then(res => {
			if(res.data.code == 20001) {
				setTimeout(() => {
					state.loading.splice(0, 1, false);
				}, 300);
				if(res.data.data) {
					commit('updateStatus', 0);
					commit('updateTotal', res.data.data['pageInfo'] && res.data.data['pageInfo']['total']);			//更新总条目数
					commit('updateTableData', res.data.data['openrationLogInfoList']);
				}
			} else {
				setTimeout(() => {
					state.loading.splice(0, 1, false);
				}, 300);
			}
		});
	}
}


export default {
	namespaced: true,
	state,
	mutations,
	actions
}